﻿using StrokeCenterSystem.Code.Bll;
using StrokeCenterSystem.Core;
using StrokeCenterSystem.ThreeCenterManage;
using StrokeCenterSystem.Utils;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;

namespace StrokeCenterSystem.Areas.StrokeCenter.Controllers
{
    public class GreenChannelController : BaseController
    {
        //
        // GET: /StrokeCenter/GreenChannel/
        /// <summary>
        /// 绿色通道审批
        /// </summary>
        /// <returns></returns>
        public ActionResult Index()
        {
            return View();
        }
        /// <summary>
        /// 加载审批小窗口
        /// </summary>
        /// <returns></returns>
        public ActionResult Approval()
        {
            string pass = WebUtils.GetString("pass");
            string id = WebUtils.GetString("id");
            ViewBag.pass = pass;
            ViewBag.id = id;
            return View();
        }
        /// <summary>
        /// 保存审批数据
        /// </summary>
        /// <returns></returns>
        public ContentResult ApprovalSave()
        {
            string pass = WebUtils.GetString("txtpass");
            string id = WebUtils.GetString("txtId");
            string approvalRemark = WebUtils.GetString("txtApprovalRemark");
            StringBuilder jsonStr = new StringBuilder();
            string[] ids = id.Split(',');
            if (ids.Length > 0)
            {
                for (int i = 0; i < ids.Length; i++)
                {
                    jsonStr.Append("{ \"ID\":\"" + ids[i] + "\",\"ApprovalStatus\":\"" + pass + "\", \"ApprovalRemark\":\"" + approvalRemark + "\" },");
                }
                string jsonResult = "[" + jsonStr.ToString().Trim(',') + "]";
                OperateResult result = GreenChannelManager.BatchApprChannel(jsonResult);
                return Content(result.Body.ToString());
            }
            return Content("操作完成");
        }

         /// <summary>
        /// 患者总览加载
        /// </summary>
        /// <returns></returns>
        public ContentResult Search()
        {
            string name = WebUtils.GetString("name");
            int status = WebUtils.GetInt("status", -1);
            int pathwayId = WebUtils.GetInt("pathway", -1);
            int pageIndex = WebUtils.GetInt("page", 1);
            int pageSize = WebUtils.GetInt("pageSize", 30);
            OperateResult resultInfo = GreenChannelManager.GetPatientOverview(pageIndex,pageSize,name, status , -1);
            string NoDate = "{\"Total\": 0,\"Rows\": [], \"InHospitalNum\": 0, \"PreHospitalNum\": 0, \"PathWayNum\": 0}";
            if (resultInfo.Result == 1)
            {
                if (resultInfo.Body.Equals("未检索到数据！")) { return Content(NoDate); }
                var jsonObj = JsonConvert.DeserializeObject<dynamic>(resultInfo.Body.ToString());
                var result = new { Total = jsonObj.PatientNum.Value, Rows = jsonObj.PatientList, InHospitalNum = jsonObj.InHospitalNum.Value, PreHospitalNum = jsonObj.PreHospitalNum.Value, PathWayNum = jsonObj.PathWayNum.Value };
                return JsonDate(result);
            }
            return Content(NoDate);
        }

        private const int BeginOpenGreenChannel = 1;
        private const int BeginCloseGreenChannel = 0;
        public ActionResult StartGreenChannel()
        {
            string patientId = WebUtils.GetString("patientId");
            int isGreenChannel = BeginOpenGreenChannel;
            OperateResult resultInfo = GreenChannelManager.StartGreenChannel(patientId, isGreenChannel);
            return JsonDate(resultInfo);
        }

        public ActionResult CloseGreenChannel()
        {
            string patientId = WebUtils.GetString("patientId");
            int isGreenChannel = BeginCloseGreenChannel;
            OperateResult resultInfo = GreenChannelManager.StartGreenChannel(patientId, isGreenChannel);
            return JsonDate(resultInfo);
        }
    }
}
